const express=require('express');
const router=express.Router();

var multer  = require('multer');
 //var upload = multer({ dest: 'public/uploads/' })

let upload = multer({
     
    storage: multer.diskStorage({
        //设置上传后文件路径，uploads文件夹会自动创建。
        destination: function (req, file, cb) {
            cb(null, 'public/uploads/');
        },
        //给上传文件重命名，获取添加后缀名
        filename: function (req, file, cb) {
            //                 2.jpg
            var fileFormat = (file.originalname).split(".");
            var changedName =  Date.now()+"."+fileFormat[fileFormat.length - 1];
            cb(null, changedName);
        }
    })
});










const query = require('../mysql')
// router.post('/Login',function(req,res){
//         console.log(req.body);
//         var  sql = `SELECT * FROM my_user WHERE name='${req.body.name}' and pwd='${req.body.pwd}'`;
//         query(sql,function (err,result,) {
//             console.log(result)
//             if(err){ //sql 语句报错
//                 console.log(err.message);
//                 res.send({code:202,data:err,msg:"错误"})
//                 return;
//             }
//             if(result.length==0){
//                 res.send({code:201,data:result,msg:"ok"})
//             }else{
//                 res.send({code:200,data:result,msg:"ok"})
//             }
//         })
// });
router.get('/getTemp',function(req,res){
    var  sql = `SELECT * FROM my_temp`;
    query(sql,function (err,result,) {
        if(err){ //sql 语句报错
            console.log(err.message);
            res.send({code:202,data:err,msg:"错误"})
            return;
        }
        if(result.length==0){
            res.send({code:201,data:[],msg:"未查到数据"})
        }else{
            res.send({code:200,data:result,msg:"ok"})
        }
    })
});

router.get('/getTempDetails',function(req,res){
    var sql=`select * from my_temp where id=${req.query.id}`;
    query(sql,function (err,result,) {
        if(err){ //sql 语句报错
            console.log(err.message);
            res.send({code:202,data:err,msg:"错误"})
            return;
        }
        if(result.length==0){
            res.send({code:201,data:[],msg:"未查到数据"})
        }else{
            res.send({code:200,data:result,msg:"ok"})
        }
    })
});


router.post('/addTemp',function(req,res){
    //console.log(req.body);
    var sql=`INSERT INTO my_temp (tempName,tempDesc) VALUES ('${req.body.tempName}','${req.body.tempDesc}')`;
    query(sql,function (err,result,) {
       // console.log(result)
        if(err){ //sql 语句报错
            console.log(err.message);
            res.send({code:202,data:err,msg:"sql语句报错"})
            return;
        }
        if(result.length==0){
            res.send({code:201,data:result,msg:"ok"})
        }else{
            res.send({code:200,data:result,msg:"ok"})
        }
    })
});
router.post('/delTemp',function(req,res){
    console.log(req.body);
    var sql=`DELETE FROM my_temp WHERE id=${req.body.id}`;
    query(sql,function (err,result,) {
        console.log(result)
        if(err){ //sql 语句报错
            console.log(err.message);
            res.send({code:202,data:err,msg:"sql语句报错"})
            return;
        }else{
            res.send({code:200,data:result,msg:"删除成功"})
        }

    })
});
router.post('/saveTemp',function(req,res){
    // console.log(req.body);
    // console.log(req.body.id)
    // console.log(req.body.temp)
    //update table set 字段=最新值 where id   UPDATE enter_user SET old_name ='111' WHERE id ='2'

    var sql=`update  my_temp set tempData='${req.body.temp}' where id=${req.body.id}`;
    query(sql,function (err,result,) {
        if(err){ //sql 语句报错
            console.log(err.message);
            res.send({code:202,data:err,msg:"sql语句报错"})
            return;
        }else{
            res.send({code:200,data:result,msg:"保存成功"})
        }

    })
});

//接收文件
router.post('/imgTemp',upload.single('file'),function(req,res){
    let avatar = req.file
    console.log(avatar)
    var sql=`update  my_temp set tempBg='${avatar.filename}' where id=${req.body.fileId}`;
    query(sql,function (err,result,) {
        if(err){ //sql 语句报错
            console.log(err.message);
            res.send({code:202,data:err,msg:"sql语句报错"})
            return;
        }else{
            res.send({code:200,data:{name:avatar.filename},msg:"成功"})
        }

    })

});


module.exports = router
